package com.lcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcn.base.BaseInfoProperties;
import com.lcn.exceptions.GraceException;
import com.lcn.grace.result.ResponseStatusEnum;
import com.lcn.mapper.FriendCircleLikedMapper;
import com.lcn.mapper.FriendCircleMapper;
import com.lcn.pojo.FriendCircle;
import com.lcn.pojo.FriendCircleLiked;
import com.lcn.pojo.Users;
import com.lcn.pojo.bo.FriendCircleBO;
import com.lcn.pojo.vo.FriendCircleVO;
import com.lcn.pojo.vo.UsersVO;
import com.lcn.service.FriendCircleService;
import com.lcn.service.UsersService;
import com.lcn.utils.PagedGridResult;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FriendCircleServiceImpl extends BaseInfoProperties implements FriendCircleService {

    @Resource
    private FriendCircleMapper friendCircleMapper;

    @Resource
    private UsersService usersService;

    @Resource
    private FriendCircleLikedMapper friendCircleLikedMapper;

    /**
     * 发布朋友圈
     *
     * @param friendCircleBO
     */
    @Override
    public void save(FriendCircleBO friendCircleBO) {
        FriendCircle friendCircle = new FriendCircle();
        BeanUtils.copyProperties(friendCircleBO, friendCircle);
        friendCircleMapper.insert(friendCircle);
    }

    /**
     * 分页查询朋友圈列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PagedGridResult queryList(String userId, Integer page, Integer pageSize) {
        Page<FriendCircleVO> friendCircleVOPage = new Page<>(page, pageSize);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userId", userId);
        friendCircleMapper.queryList(friendCircleVOPage, paramMap);
        return setterPagedGridPlus(friendCircleVOPage);
    }

    /**
     * 点赞
     *
     * @param friendCircleId
     */
    @Override
    public void like(String friendCircleId, String userId) {
        FriendCircleLiked friendCircleLiked = new FriendCircleLiked();
        FriendCircle friendCircle = getFriendCircleById(friendCircleId);
        if (friendCircleId == null) {
            GraceException.display(ResponseStatusEnum.EMPTY_FRIEND_CIRCLE);
        }
        friendCircleLiked.setBelongUserId(friendCircle.getUserId());
        friendCircleLiked.setFriendCircleId(friendCircleId);
        friendCircleLiked.setLikedUserId(userId);

        Users users = usersService.getById(userId);

        friendCircleLiked.setLikedUserName(users.getNickname());
        friendCircleLiked.setCreatedTime(LocalDateTime.now());

        friendCircleLikedMapper.insert(friendCircleLiked);

        // redis计数
        redis.increment(REDIS_FRIEND_CIRCLE_LIKED_COUNTS, 1);
        // 标记点赞的用户
        redis.setnx(REDIS_DOES_USER_LIKE_FRIEND_CIRCLE + ":" + userId + ":" + friendCircleId, userId);
    }

    /**
     * 取消点赞
     *
     * @param friendCircleId
     */
    @Override
    public void unlike(String friendCircleId, String userId) {
        QueryWrapper<FriendCircleLiked> queryWrapper = new QueryWrapper<FriendCircleLiked>().eq("friend_circle_id", friendCircleId);
        friendCircleLikedMapper.delete(queryWrapper);
        // 删除缓存
        redis.decrement(REDIS_FRIEND_CIRCLE_LIKED_COUNTS, 1);
        redis.del(REDIS_DOES_USER_LIKE_FRIEND_CIRCLE + ":" + userId + ":" + friendCircleId);
    }

    /**
     * 查询朋友圈点赞列表
     *
     * @param friendCircleId
     * @return
     */
    @Override
    public List<FriendCircleLiked> queryFriendCircleLikedList(String friendCircleId) {
        QueryWrapper<FriendCircleLiked> queryWrapper = new QueryWrapper<FriendCircleLiked>()
                .eq("friend_circle_id", friendCircleId);
        return friendCircleLikedMapper.selectList(queryWrapper);
    }

    /**
     * 判断是否点赞
     *
     * @param friendCircleId
     * @param userId
     * @return
     */
    @Override
    public boolean doILike(String friendCircleId, String userId) {
        String s = redis.get(REDIS_DOES_USER_LIKE_FRIEND_CIRCLE + ":" + userId + ":" + friendCircleId);
        return StringUtils.isNotBlank(s);
    }

    /**
     * 根据id查朋友圈
     *
     * @param friendCircleId
     * @return
     */
    private FriendCircle getFriendCircleById(String friendCircleId) {
        return friendCircleMapper.selectById(friendCircleId);
    }

}
